package springboot.bigdata.demo.dao.impl;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springboot.bigdata.demo.dao.UserExpenseDao;
import springboot.bigdata.demo.mappers.db0.DB0UserExpenseMapper;
import springboot.bigdata.demo.mappers.db1.DB1UserExpenseMapper;
import springboot.bigdata.demo.model.UserExpense;

import java.util.List;

@Component
public class UserExpenseDaoImpl implements UserExpenseDao {

    @Autowired
    private DB0UserExpenseMapper db0UserExpenseMapper;
    @Autowired
    private DB1UserExpenseMapper db1UserExpenseMapper;

    @Override
    public UserExpense addUserExpense(UserExpense userExpense) {
        if (userExpense.getExpenseId() %2 == 0){
            db0UserExpenseMapper.insertSelective(userExpense);
        }else {
            db1UserExpenseMapper.insertSelective(userExpense);
        }

        return userExpense;
    }

    @Override
    public int addList(List<UserExpense> userExpenseList, int dbType) {

        if (CollUtil.isEmpty(userExpenseList)){
            return 0;
        }

        if (dbType == 0){
            db0UserExpenseMapper.insertSelectiveList(userExpenseList);
        }else if (dbType == 1){
            db1UserExpenseMapper.insertSelectiveList(userExpenseList);
        }

        return 0;
    }
}
